import { PetStatusParams as Params } from '@swda/configs/params'
import { setGlobal } from '@swda/utils'


let SWDA_PetStatusWindow = {}

//=============================================================================
// BattleManager
//=============================================================================
SWDA_PetStatusWindow.BattleManager_InitMembers = BattleManager.initMembers;
BattleManager.initMembers = function () {
    SWDA_PetStatusWindow.BattleManager_InitMembers.call(this);
    this._petWindowA = null;
    this._petWindowB = null;
};

/*
SWDA_PetStatusWindow.BattleManager_RefreshStatus = BattleManager.refreshStatus;
BattleManager.refreshStatus = function () {
    SWDA_PetStatusWindow.BattleManager_RefreshStatus.call(this);
};
*/
BattleManager.setPetWindowA = function (petWindow) {
    this._petWindowA = petWindow;
};
BattleManager.setPetWindowB = function (petWindow) {
    this._petWindowB = petWindow;
};
/*
SWDA_PetStatusWindow.BattleManager_ActionRefreshStatus = BattleManager.actionRefreshStatus;
BattleManager.actionRefreshStatus = function () {
    //this._petWindow.refresh();//【没用】
    SWDA_PetStatusWindow.BattleManager_ActionRefreshStatus.call(this);
};
*/
//=============================================================================
// Game_Actor
//=============================================================================
SWDA_PetStatusWindow.GameActor_BattleStatusWindowRefresh = Game_Actor.prototype.battleStatusWindowRefresh;
Game_Actor.prototype.battleStatusWindowRefresh = function () {
    SWDA_PetStatusWindow.GameActor_BattleStatusWindowRefresh.call(this);
    if (!$gameParty.battlePets().contains(this)) return;
};






//=============================================================================
// Window_PetBattleStatus【战斗状态】【重要】
//=============================================================================
function Window_PetBattleStatus() {
    this.initialize.apply(this, arguments);
}

Window_PetBattleStatus.prototype = Object.create(Window_Selectable.prototype);
Window_PetBattleStatus.prototype.constructor = Window_PetBattleStatus;


Window_PetBattleStatus.prototype.initialize = function (x, y, index) {
    let width = this.windowWidth();
    let height = this.windowHeight();
    Window_Selectable.prototype.initialize.call(this, x, y, width, height);
    this._currentMax = Params.BSWCurrentMax;
    this._pet = null;
    this._petIndex = index;
    this.openness = 0;
    this.refresh();
    this.windowskin = ImageManager.loadSystem('Window3');
};

Window_PetBattleStatus.prototype.update = function () {
    Window_Selectable.prototype.update.call(this);
    this.updatePosition();
}

Window_PetBattleStatus.prototype.updatePosition = function () {
    // 行动时下移
    let actor = this.pet();
    if (!!actor){
        if (
            (actor.currentAction() && actor.currentAction().item()) && 
            actor.atbRate() >= 1 && 
            !(actor.isATBCharging() && 
            actor.atbChargeRate() < 1)
        ) {
            if (this.y <= 16)
                this.y += 2;
        } else {
            if (this.y > 2)
                this.y -= 2;
        }
    }
}


Window_PetBattleStatus.prototype.setPet = function () {
    if (!$gameParty.battlePets()[this._petIndex]){
        this._pet = null; 
        return;
    } 
    this._pet = $gameParty.battlePets()[this._petIndex];
}

Window_PetBattleStatus.prototype.pet = function () {
    return this._pet;
}


// 基本尺寸信息
Window_PetBattleStatus.prototype.standardPadding = function () {
    return 8;
}
// 基本尺寸信息
Window_PetBattleStatus.prototype.windowWidth = function () {
    return 180 + this.standardPadding() * 2;
};
Window_PetBattleStatus.prototype.windowHeight = function () {
    return this.itemHeight() + this.standardPadding() * 2;
};
Window_PetBattleStatus.prototype.maxCols = function () {
    return 1;
};
Window_PetBattleStatus.prototype.maxItems = function () {
    return 1;
};
Window_PetBattleStatus.prototype.numVisibleRows = function () {
    return this.maxItems();
};
Window_PetBattleStatus.prototype.itemWidth = function () {
    return this.contents.width / this.maxCols();
};

Window_PetBattleStatus.prototype.itemHeight = function () {
    return this.lineHeight() * 2;

};
// 刷新相关=============================================
Window_PetBattleStatus.prototype.refresh = function () {
    this.setPet();
    
    if (!!this.pet()){
        this.contents.clear();
        this.drawAllItems();
        this.pet().completetStatusRefreshRequest();
        // 仅在行动执行时触发护驾窗口的开启
        if(BattleManager._phase === 'actionList')
            this.open();
    } else{
        this.close();
    }
};



Window_PetBattleStatus.prototype.drawItem = function () {
    this.drawBasicArea(this.basicAreaRect(0), this.pet());
    this.drawGaugeArea(this.gaugeAreaRect(0), this.pet());
};


Window_PetBattleStatus.prototype.drawBasicArea = function (rect, actor) {
    // 不行动时不画行动图标
    if (actor.atbRate()>=1 && !(actor.isATBCharging() && actor.atbChargeRate()<1)) {
        this.drawActorActionIcon(actor, rect.width - Window_Base._iconWidth, 0);
    }
    this.resetFontSettings();
    this.setSmallerFont();
    this.contents.fontSize = Params.BSWNameFontSize;
    this.drawActorName(actor, rect.x, rect.y, rect.width);
    this.resetFontSettings();
};

Window_PetBattleStatus.prototype.drawActorName = function(actor, x, y, width) {
    width = width || 168;
    this.changeTextColor(this.hpColor(actor));
    this.drawText(actor.name().split("（")[0], x, y, width);
};

// 人物信息颜色
Window_PetBattleStatus.prototype.hpColor = function (actor) {
    if (actor.isDead()) {
        return this.deathColor();
    } else if (actor.isDying()) {
        return this.crisisColor();
    } else {
        return this.textColor(15);
    }
};



Window_PetBattleStatus.prototype.mpColor = function () {
    return this.textColor(15);
};


Window_PetBattleStatus.prototype.tpColor = function () {
    return this.textColor(15);
};


// 基本区域Rect
Window_PetBattleStatus.prototype.basicAreaRect = function () {
    const rect = this.itemRectForText(0);
    return rect;
};

Window_PetBattleStatus.prototype.drawGaugeArea = function (rect, actor) {
    this._enableYBuffer = true;
    let wy = rect.y + rect.height - this.lineHeight() * 2;
    this.drawActorMp(actor, rect.x, wy, rect.width /2 - this.standardPadding());
    this.drawActorTp(actor, rect.x + rect.width/2 + this.standardPadding(), wy, rect.width/2 - this.standardPadding());
    this._enableYBuffer = false;
};




Window_PetBattleStatus.prototype.gaugeAreaRect = function () {
    let rect = this.itemRectForText(0);
    rect.y = rect.y + this.lineHeight();
    return rect;
};

Window_PetBattleStatus.prototype.drawCurrentAndMax = function (current, max, x, y,
    width, color1, color2) {
        this.setSmallerFont();
    this.contents.fontSize =  Params.BSWParamFontSize;
    if (this._currentMax) {
        Window_Selectable.prototype.drawCurrentAndMax.call(this, current, max,
            x, y + 3, width, color1, this.textColor(15));	// 修改最大值颜色
    } else {
        this.changeTextColor(color1);
        let value = SWDA_PetStatusWindow.Util.toGroup(current);
        this.drawText(value, x, y + 3, width, 'right');
    }
    this.resetFontSettings();
};




//=============================================================================
// Scene_Battle【战斗场景】
//=============================================================================
SWDA_PetStatusWindow.SceneBattle_CreateStatusWindow = Scene_Battle.prototype.createStatusWindow;
Scene_Battle.prototype.createStatusWindow = function () {
    SWDA_PetStatusWindow.SceneBattle_CreateStatusWindow.call(this);
    this._petWindowA = new Window_PetBattleStatus(0, 0, 0);
    this._petWindowB = new Window_PetBattleStatus(this._petWindowA.width, 0, 1);
    this.addWindow(this._petWindowA);
    this.addWindow(this._petWindowB);
};

SWDA_PetStatusWindow.Scene_Battle_Update = Scene_Battle.prototype.update;
Scene_Battle.prototype.update = function () {
    SWDA_PetStatusWindow.Scene_Battle_Update.call(this);
    this.updatePetWindowRequests();
};

// 更新状态窗口请求
Scene_Battle.prototype.updatePetWindowRequests = function () {
    if (!this._petWindowA || !this._petWindowB) return;

    if (BattleManager._victoryPhase) {
        this._petWindowA.close();
        this._petWindowB.close();
    }
    if (BattleManager._victoryPhase && (this._petWindowA.isClosed()||this._petWindowA.isClosed())) {
        return;
    }
	for (var i = 0; i < $gameParty.battlePets().length; ++i) {	
        var actor = $gameParty.battlePets()[i];
        if (!actor) continue;
        if (actor.isStatusRefreshRequested()){
            this._petWindowA.refresh();
            this._petWindowB.refresh();
            actor.
            continue;
        }
      }
};


/*
SWDA_PetStatusWindow.SceneBattle_RefreshStatus = Scene_Battle.prototype.refreshStatus;
Scene_Battle.prototype.refreshStatus = function () {
    SWDA_PetStatusWindow.SceneBattle_RefreshStatus.call(this);
};
*/
SWDA_PetStatusWindow.SceneBattle_CreateDisplayObjects = Scene_Battle.prototype.createDisplayObjects;
Scene_Battle.prototype.createDisplayObjects = function () {
    SWDA_PetStatusWindow.SceneBattle_CreateDisplayObjects.call(this);
    BattleManager.setPetWindowA(this._petWindowA);
    BattleManager.setPetWindowB(this._petWindowB);
};

/*
SWDA_PetStatusWindow.SceneBattle_StartActorCommandSelection = Scene_Battle.prototype.startActorCommandSelection;
Scene_Battle.prototype.startActorCommandSelection = function () {
    SWDA_PetStatusWindow.SceneBattle_StartActorCommandSelection.call(this);
    //this._petWindow.refresh();//【没用】
};
*/



Window_PetList.prototype.drawItem = function(index){
	var pet = this._list[index];
	var rect = this.itemRect(index);

	this.resetTextColor();
	if(pet.isInBattle())
		this.changeTextColor(this.fightColor());
	this.drawText(pet.name().split("（")[0], rect.x, rect.y, rect.width / 2, "left");   //NSWDA：去掉括号

	if(this.canShowPetLevel())
		this.drawText(CP.PetManager.PET_LEVEL_NAME + pet.level, rect.x + rect.width / 2, rect.y
            , rect.width / 2, "right");
};

//=============================================================================
// Utilities
//=============================================================================

SWDA_PetStatusWindow.Util = SWDA_PetStatusWindow.Util || {};

if (!SWDA_PetStatusWindow.Util.toGroup) {
    SWDA_PetStatusWindow.Util.toGroup = function (inVal) {
        return inVal;
    }
};


setGlobal('Window_PetBattleStatus', Window_PetBattleStatus)